<?php

declare(strict_types=1);

namespace ImiApp\WebSocketServer\Controller;

 
use Imi\ConnectionContext;
use Imi\Controller\WebSocketController;
use Imi\Server\WebSocket\Route\Annotation\WSRoute;
// use Imi\Server\WebSocket\Route\Annotation\WSConfig;
use Imi\Server\WebSocket\Route\Annotation\WSAction;
use Imi\Server\WebSocket\Route\Annotation\WSController;

use Imi\Config;
use Imi\Db\Db;
 
/**
 * 数据收发测试
 * @WSController
 */
class Auction extends WsBase
{

    //auction_join_group 用户加入拍卖群
    //auction_show 拍卖到预展区(新加的拍卖)
    //auction_offer 拍卖出价
    //auciton_start 拍卖开始
    //auciton_finish 拍卖结束
    //auction_update_end_time 更新结束时间

    /**
     * 加入拍卖群
     *
     * @WSAction
     * @WSRoute({"action"="auction.join"})
     * @param mixed $data
     * @return void
     */
    public function join($data)
    {
        var_dump('auction.join加入拍卖群'.ConnectionContext::get('user_id'));
        $user_id = ConnectionContext::get('user_id');
        if(!isset($data['auction_id'])){
            return ['code'=>-1,'msg'=>'缺少拍卖参数'];
        }
        $auction_id = $data['auction_id'];
        $群组名 =  'auction_'.$auction_id;
        $auction = Db::name('auction')->where('id','=',$auction_id)->find();
        if(!$auction){
            return $this->error('没有该拍卖');
        }

        $this->server->joinGroup($群组名, $this->frame->getClientId());

        $this->server->groupCall($群组名, 'push', $this->success('用户'.$user_id.'加入成功',[],'auction_join_group'));
      
        return $this->success('加入成功',['auction'=>$auction,'count'=>$this->server->getGroup($群组名)->count()]);
    }   
    
    

    /**
     * 离开拍卖群
     *
     * @WSAction
     * @WSRoute({"action"="auction.leave"})
     * @param mixed $data
     * @return void
     */
    public function leave($data)
    {
        $user_id = ConnectionContext::get('user_id');
        if(!isset($data['auction_id'])){
            return ['code'=>-1,'msg'=>'缺少拍卖参数'];
        }
        $auction_id = $data['auction_id'];
        $群组名 =  'auction_'.$auction_id;
        $res = $this->server->leaveGroup($群组名, $this->frame->getClientId());      
        return $this->success('离开成功',['res'=>$res]);
    }  


     /**
     * 加入拍卖列表
     *
     * @WSAction
     * @WSRoute({"action"="auction.joinlist"})
     *
     * @param mixed $data
     */
    public function joinlist($data): array
    {
   
        $this->server->joinGroup(WS_拍卖列表, $this->frame->getClientId());
        return $this->success('拍卖列表',['count'=>$this->server->getGroup(WS_拍卖列表)->count()]);
    }

    /**
     * 离开拍卖列表
     *
     * @WSAction
     * @WSRoute({"action"="auction.leavelist"})
     *
     * @param mixed $data
     */
    public function leavelist($data): array
    {   
        $this->server->leaveGroup(WS_拍卖列表, $this->frame->getClientId());
        return $this->success('离开拍卖列表');
    }
}